package com.glut.gultguy.service.impl;

import com.glut.gultguy.To.CommentTo;
import com.glut.gultguy.entity.Comment;
import com.glut.gultguy.mapper.CommentMapper;
import com.glut.gultguy.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glut.gultguy.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-05
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired(required = false)
    private CommentMapper mapper;

    @Autowired(required = false)
    private SnowflakeIdWorker idWorker;
    @Override
    public Map<String,Integer> addComment(CommentTo to) {
        Comment comment = new Comment();
        comment.setComment(to.getComtent());
        comment.set_id(idWorker.nextId()+"");
        comment.set_openid(to.getUser().get_openid());
        comment.setArticle_id(to.getArticleId());
        comment.setPublishTime(new Date());
        if (to.getParentId()!=null){
            comment.setParent_id(to.getParentId());
        }
       int t = mapper.insert(comment);
        Map<String,Integer> map = new HashMap<>();
        if (t==1){
            map.put("code",200);
        }else {
            map.put("code",500);
        }
        return map;
    }
}
